<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}-
                {{ adminInfo.currentStore.name }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('medicalStaff.name')"
                        type="string"
                        v-model="baTable.form.items!.name"
                        prop="name"
                        :placeholder="t('Please input field', { field: t('medicalStaff.name') })"
                    />
                    <FormItem
                        label="角色"
                        v-model="baTable.form.items!.role_ids"
                        prop="role_ids"
                        type="remoteSelect"
                        :key="'role-' + baTable.form.items!.id"
                        :input-attr="{
                            multiple: true,
                            field: 'name',
                            remoteUrl: '/admin/Role/index',
                            placeholder: '点击选择角色',
                        }"
                    />
                    <!-- <el-form-item label="角色" prop="role_ids" required> 
                        <el-select v-model="baTable.form.items!.role_ids" multiple filterable remote reserve-keyword
                                   :remote-method="remoteSearchRoles" :loading="roleLoading" placeholder="请选择角色">
                            <el-option v-for="role in roleOptions" :key="role.id" :label="role.name" :value="role.id" />
                        </el-select>
                    </el-form-item> -->
                    <FormItem :label="t('medicalStaff.avatar')" type="image" v-model="baTable.form.items!.avatar" prop="avatar" />
                    <FormItem
                        :label="t('medicalStaff.gender')"
                        type="radio"
                        v-model="baTable.form.items!.gender"
                        prop="gender"
                        :input-attr="{ content: { '1': t('medicalStaff.gender 1'), '2': t('medicalStaff.gender 2') } }"
                        :placeholder="t('Please select field', { field: t('medicalStaff.gender') })"
                    />
                    <!-- <FormItem
                        :label="t('medicalStaff.title')"
                        type="string"
                        v-model="baTable.form.items!.title"
                        prop="title"
                        :placeholder="t('Please input field', { field: t('medicalStaff.title') })"
                    /> -->
                    <FormItem
                        :label="t('medicalStaff.specialty')"
                        type="textarea"
                        v-model="baTable.form.items!.specialty"
                        prop="specialty"
                        :input-attr="{ rows: 3 }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('medicalStaff.specialty') })"
                    />
                    <FormItem
                        :label="t('medicalStaff.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :input-attr="{ content: { '1': t('medicalStaff.status 1'), '0': t('medicalStaff.status 0') } }"
                        :placeholder="t('Please select field', { field: t('medicalStaff.status') })"
                    />

                    <!-- <FormItem
                        :label="t('medicalStaff.contact_phone')"
                        type="string"
                        v-model="baTable.form.items!.contact_phone"
                        prop="contact_phone"
                        :placeholder="t('Please input field', { field: t('medicalStaff.contact_phone') })"
                    /> -->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormItemRule } from 'element-plus'
import { inject, reactive, ref, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import { useAdminInfo } from '/@/stores/adminInfo'
import createAxios from '/@/utils/axios'

const adminInfo = useAdminInfo()
const config = useConfig()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    name: [buildValidatorData({ name: 'required', title: t('medicalStaff.name') })],
    role_ids: [buildValidatorData({ name: 'required', message: t('Please select field', { field: t('角色') }) })],
    created_at: [buildValidatorData({ name: 'date', title: t('medicalStaff.created_at') })],
    updated_at: [buildValidatorData({ name: 'date', title: t('medicalStaff.updated_at') })],
})

// 角色选项与远程搜索
// const roleOptions = ref<{ id: number; name: string }[]>([])
// const roleLoading = ref(false)
// function remoteSearchRoles(query: string) {
//     roleLoading.value = true
//     createAxios({
//         url: '/admin/Role/index',
//         method: 'get',
//         params: { quickSearch: query, select: true },
//     }).then((res) => {
//         roleOptions.value = res.data.list
//         roleLoading.value = false
//     }).catch(() => { roleLoading.value = false })
// }
</script>

<style scoped lang="scss"></style>
